const fs = require("fs");
const path = require("path");

module.exports = function(context){

    const dependency = '' +
        '        // ADD-TINKER-DEPENDENCY-START\n' +
        '        classpath "com.tencent.bugly:tinker-support:1.2.3"\n' +
        '        // classpath "com.android.tools.build:gradle:3.4.0"\n' +
        '        // ADD-TINKER-DEPENDENCY-END\n'

    const androidRoot = path.join(context.opts.projectRoot, "platforms/android")
    const modifyTag = '// in the individual module build.gradle files';
    const buildFile = path.join(androidRoot, "build.gradle");
    const fileContents = fs.readFileSync(buildFile, "utf8");

    if(fileContents.indexOf('ADD-TINKER-DEPENDENCY') < 0){
        const lines = fileContents.split(/\n/)
        let newContent = ''
        for(const line of lines){
            // console.log('line:'+line)
            newContent += (line + '\n')
            if(line.indexOf(modifyTag) >=0 ){
                newContent += dependency
            }
        }
        fs.writeFileSync(buildFile, newContent, "utf8");
        console.log("updated " + buildFile + " to add tinker dependency.");
    }

};
